package engine.network.hostSide.mp;

import java.awt.Point;
import java.io.IOException;
import java.net.Socket;

import engine.network.hostSide.abstractClasses.HandlingThread;
import engine.network.hostSide.abstractClasses.PositionDispatcher;
import engine.network.hostSide.enums.PlayerReq;
import graphicUserInterface.elements.PositionListener;

public class HINBNetworkPlayer 
			extends HandlingThread implements PositionDispatcher{
	/* *********************************************************
	 * Attributes
	 * ********************************************************* */
	private HINBPlayer _player;
	
	
	/* *********************************************************
	 * Constructor(s)
	 * ********************************************************* */
	HINBNetworkPlayer(Socket s){
		super(s);
		_player = new HINBPlayer();
		this.start();
	}
	
	/* *********************************************************
	 * Instance Methods
	 * ********************************************************* */

	@Override
	public void run() {
		//todo readline and set The new position
		try {
			PlayerReq req;
			do{
				System.out.println("waiting for newCoord");
				String newreq = _in.readLine();
				req = PlayerReq.factory(newreq);
				if(req == PlayerReq.NEW_COORD){
					int x = Integer.parseInt(_in.readLine());
					int y = Integer.parseInt(_in.readLine());
					System.out.println("new point recieved : "+x+"x"+y);
					_player.setNewPoint(new Point(x, y));
					
				}
			}while(req != PlayerReq.EXIT);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public Point getPoint() {
		return _player.getPoint();
	}

	@Override
	public void attach(PositionListener up) {
		_player.attach(up);
	}

	@Override
	public void detach(PositionListener up) {
		_player.detach(up);
	}
	
}
